package com.happyfamily.springboot.crowdsourcingplatform.controller.guo;

import com.google.gson.Gson;
import com.happyfamily.springboot.crowdsourcingplatform.annotation.OperationLogDetail;
import com.happyfamily.springboot.crowdsourcingplatform.enums.OperationType;
import com.happyfamily.springboot.crowdsourcingplatform.enums.OperationUnit;
import com.happyfamily.springboot.crowdsourcingplatform.model.*;
import com.happyfamily.springboot.crowdsourcingplatform.service.guo.FriendService;
import com.happyfamily.springboot.crowdsourcingplatform.service.guo.FrontNeedService;
import com.happyfamily.springboot.crowdsourcingplatform.service.guo.ServiceProvidersService;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.session.RowBounds;
import org.hibernate.validator.internal.constraintvalidators.bv.time.futureorpresent.FutureOrPresentValidatorForReadableInstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextImpl;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/api-front/serviceproviders")
public class ServiceController
{
	@Autowired
	private ServiceProvidersService service;

	@Autowired
	private FrontNeedService frontNeedService;


	@GetMapping("/list")
	@ResponseBody
	@ApiOperation(value = "服务商获取订单列表", notes = "服务商获取订单列表")
	@OperationLogDetail(detail = "服务商获取订单列表", level = 1, operationUnit = OperationUnit.USER, operationType = OperationType.SELECT)
	public String getServerByPage(HttpServletRequest request, String page)
	{
		SecurityContextImpl securityContext = (SecurityContextImpl) request.getSession().getAttribute("SPRING_SECURITY_CONTEXT");
		String account = securityContext.getAuthentication().getName();
		TableModel tableModel = new TableModel();
		List<Orders> list = service.getTenderList(account);
		Date d = new Date();
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		String currdate = format.format(d);
		for (int i = 0; i < list.size(); i++)
		{
			if (list.get(i).getWorkid() == 0)
			{
				list.get(i).setTips2(frontNeedService.getNeed(list.get(i).getNeedid()).getNmoney());
				list.get(i).setTips1(frontNeedService.getParameterTypeOne(list.get(i).getPid()).getPname());
				list.get(i).setTips3(currdate);
			} else
			{
				list.get(i).setTips2(frontNeedService.getWork(String.valueOf(list.get(i).getWorkid())).getWmoney());
				list.get(i).setTips1(frontNeedService.getParameterTypeOne(list.get(i).getPid()).getPname());
				list.get(i).setTips3(currdate);
			}

		}


		System.out.println(list);
		Integer p = Integer.valueOf(page);
		int sum = list.size() - 1;
		int x = 5 * (p - 1);
		if (x + 4 <= sum)
		{
			sum = x + 4;
		}
		List<Orders> list2 = new ArrayList<>();
		for (int i = 5 * (p - 1); i <= sum; i++)
		{
			list2.add(list.get(i));
		}
		tableModel.setData(list2);
		tableModel.setCode(0);
		tableModel.setMsg("");
		tableModel.setCount(list.size());
		Gson gson = new Gson();
		return gson.toJson(tableModel);


	}

	@GetMapping("/delivery")
	@ResponseBody
	@ApiOperation(value = "服务商发货", notes = "服务商发货")
	@OperationLogDetail(detail = "服务商发货", level = 5, operationUnit = OperationUnit.USER, operationType = OperationType.ALL)
	public String delivery(HttpServletRequest request, String nid, String oid)
	{
		SecurityContextImpl securityContext = (SecurityContextImpl) request.getSession().getAttribute("SPRING_SECURITY_CONTEXT");
		String account = securityContext.getAuthentication().getName();
		return frontNeedService.delivery(nid, oid, account);
	}


	@GetMapping("/orderForm")
	public String orderForm(Model model,String oid)
	{
		model.addAttribute("oid", oid);
		return "frontPage/guoorder-info";
	}

	@GetMapping("/getOrderForm")
	@ResponseBody
	@ApiOperation(value = "前端获取订单流程操作", notes = "前端获取订单流程操作")
	@OperationLogDetail(detail = "前端获取订单流程操作", level = 1, operationUnit = OperationUnit.USER, operationType = OperationType.SELECT)
	public List<OrderForm> getOrderForm(String oid){
		List<OrderForm> orderForms=service.getOrderForm(Long.valueOf(oid));
		for (int i = 0; i <orderForms.size(); i++)
		{
			orderForms.get(i).setTips1(String.valueOf(frontNeedService.getOrder(Long.valueOf(oid)).getPid()));
		}
		return service.getOrderForm(Long.valueOf(oid));
	}

	@GetMapping("/details")
	public String userDetail(Model model, String wid)
	{
		model.addAttribute("work", frontNeedService.getWork(wid));
		return "frontPage/guoworkdetails";
	}

}
